package com.getword.web.controller;

import com.getword.domain.Outline;
import com.getword.domain.User;
import com.getword.security.SUser;
import com.getword.service.OutlineService;
import com.getword.web.model.Result;
import com.getword.web.model.ResultCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/outline")
public class ApiOutlineController {
    @Autowired
    private OutlineService outlineService;


    /**
     * 文件上传，解析暂存到session中
     * {
     * "code": 0
     * ,"msg": ""
     * ,"data": {
     * "src": "http://cdn.layui.com/123.jpg"
     * }
     * }
     *
     * @param file
     * @return
     */
    @PostMapping("/file")
    public Object upload(MultipartFile file, Outline outline, HttpServletRequest request) {
        SUser user = (SUser) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        outline.setUser(new User(user.getUserId()));

        Result result1 = outlineService.getOutlineData(file, request);
        Map<String, Object> result = new HashMap<>();
        result.put("code", 0);
        result.put("msg", "ok");
        result.put("data", new HashMap<String, String>() {{
        }});
        return result1;
    }

    @GetMapping("/preview")
    public Result preview(HttpServletRequest request) {
        // session中的数据不能修改，带保存到数据库中
        final List<Map<String, Object>> outline = (List<Map<String, Object>>) request.getSession().getAttribute("outline");
        if (outline == null) {
            return Result.fail(ResultCode.SERVER_ERROR.getCode(), "数据为上传", null);
        }
        Result result = Result.success(outline, null);
        return result;
    }
    // 修改大纲内容
    @PostMapping("/updateSessionOutline")
    public Result updateSessionOutline(Integer id, String name, String intro, HttpServletRequest request){
        return outlineService.updateSessionOutline(id, name, intro, request);
    }

    @PostMapping
    public Result create(Outline outline, HttpServletRequest request) {
        System.out.println(outline);
        List<Map<String, Object>> content = (List<Map<String, Object>>) request.getSession().getAttribute("outline");
        if (content == null) {
            return Result.fail(ResultCode.SERVER_ERROR.getCode(), "请上传大纲文件", null);
        }
        SUser user = (SUser) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        outline.setUser(new User(user.getUserId()));
        return outlineService.saveOutline(outline, request);
    }

    // 判断大纲名字是否重复
    @PostMapping("/checkName")
    public Result checkName(Long courseId, String name) {

        return outlineService.checkName(courseId, name);
    }

    // 查询某课程的大纲
    @GetMapping
    public Result getByCourseId(Long courseId) {
        if (courseId == null) {
            return Result.fail("参数不合法");
        }
        return outlineService.getOutlineByCourseId(courseId);
    }

    // 获取大纲结构，tree
    @GetMapping("/{id:\\d+}")
    public Result getOutlineInfo(@PathVariable Long id) {
        return outlineService.getOutlineById(id);
    }

    // 选择大纲知识点
//    @GetMapping("/detail")
//    public Object getOutlineDetail(Long pid){
//        if(pid == null){
//            return new HashMap<String, Object>(){{
//               put("id",1);
//               put("name", "1111");
//               put("extands", true);
//               put("pid", -1);
//            }};
//        }
//        return null;
//    }

    // 大纲审核通过
    @PostMapping("/pass")
    public Result pass(Long id){
        return outlineService.pass(id);
    }

    // 大纲查询，当前用户
    @GetMapping("/query")
    public Object query(int page, int size, Outline outline, HttpServletRequest request){
        Pageable pageable = PageRequest.of(page-1, size);
        SUser user = (SUser) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        return outlineService.getOutlinePage(pageable, outline, user.getUserId());
    }

    @GetMapping("outlineQueryAll")
    public Object outlineQueryAll(int page, int size, Outline outline){
        Pageable pageable = PageRequest.of(page-1, size);
        return outlineService.getOutlinePageAllLay(pageable, outline);
    }

    // 大纲查询，所有,admin
    @GetMapping("/queryAll")
    public Result queryAll(int page, int size, Outline outline){
        Pageable pageable = PageRequest.of(page-1, size);
        return outlineService.getOutlinePageAll(pageable, outline);
    }

    // 大纲查询，所有
    @GetMapping("/allCount")
    public Result allCount(Outline outline){
        return outlineService.allCount(outline);
    }

    // 大纲demo,模板
    @GetMapping("/demo")
    public Result demo(){
        return outlineService.demo();
    }
}
